﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Game.Foundation.ChineseChess;
using System.Drawing;
using System.Windows.Forms;

namespace Game.Client.ChineseChess
{
    class ChessboardClient : Chessboard
    {
        public Point[,] Points { get; private set; }

        public static readonly int CellSize = 50;

        public ChessboardClient(Control container)
            : base()
        {
            this.Points = new Point[Chessboard.RowCount, Chessboard.ColumnCount];
            for (int i = 0; i < Chessboard.RowCount; i++)
            {
                for (int j = 0; j < Chessboard.ColumnCount; j++)
                {
                    Points[i, j].X = 50 + j * CellSize;
                    Points[i, j].Y = 50 + i * CellSize;
                }
            }

            container.Controls.Clear();

            foreach (var item in this._chessmanList)
            {
                Image image = new Bitmap(CellSize, CellSize);
                using (Graphics g = Graphics.FromImage(image))
                {
                    Brush brush = new SolidBrush(Color.White);
                    g.FillEllipse(brush, new Rectangle(new Point(0, 0), new Size(CellSize, CellSize)));
                    g.DrawString(item.DisplayText, new Font("隶书", 20), new SolidBrush(item.Camp == Camp.Black ? Color.Black : Color.Red), 8, 10);
                }
                Label label = new Label();
                label.BackColor = Color.Transparent;
                label.Size = new Size(CellSize, CellSize);
                label.Image = image;
                label.Location = new Point(this.Points[item.YLocation - 1, item.XLocation - 1].X - CellSize / 2, this.Points[item.YLocation - 1, item.XLocation - 1].Y - CellSize / 2);
                label.BackgroundImageLayout = ImageLayout.Stretch;
                label.Tag = item;
                item.Tag = label;

                container.Controls.Add(label);
            }
        }

        public override bool MoveChessman(ChessmanBase chess, int Xdest, int Ydest)
        {
            if (!this._chessmanList.Contains(chess) || chess == null) return false;

            if (chess.CanMoveTo(Xdest, Ydest))
            {
                ChessmanBase dest = this.GetChessman(Xdest, Ydest);
                if (dest != null && dest.Camp != chess.Camp)
                {
                    this._chessmanList.Remove(dest);
                    (dest.Tag as Label).Dispose();
                    if (dest is Shuai)
                    {
                        this.OnComplete();
                    }
                }

                chess.SetLocation(Xdest, Ydest);
                (chess.Tag as Label).Location = new Point(this.Points[Ydest - 1, Xdest - 1].X - CellSize / 2, this.Points[Ydest - 1, Xdest - 1].Y - CellSize / 2);
                return true;
            }

            return false;
        }

        public bool GetAssociateLocation(Point mouseLocation, out int x, out int y)
        {
            x = 0;
            y = 0;
            for (int yLocation = 0; yLocation < Chessboard.RowCount; yLocation++)
            {
                for (int xLocation = 0; xLocation < Chessboard.ColumnCount; xLocation++)
                {
                    double distance = Math.Sqrt((this.Points[yLocation, xLocation].X - mouseLocation.X) * (this.Points[yLocation, xLocation].X - mouseLocation.X) + (this.Points[yLocation, xLocation].Y - mouseLocation.Y) * (this.Points[yLocation, xLocation].Y - mouseLocation.Y));
                    if (distance <= CellSize / 3)
                    {
                        x = xLocation + 1;
                        y = yLocation + 1;
                        //MessageBox.Show(string.Format("行{1}列{0}距离{2}", x, y, distance));
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
